package com.hui.config.security.wechat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

@Component
public class AuthCenterAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    private AuthCenterLoginSuccessHandler loginSuccessHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public void configure(HttpSecurity http) {
        //自定义SmsCodeAuthenticationFilter过滤器
        AuthCenterAuthenticationFilter authCenterAuthenticationFilter = new AuthCenterAuthenticationFilter();
        authCenterAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        authCenterAuthenticationFilter.setAuthenticationSuccessHandler(loginSuccessHandler);

        //设置自定义SmsCodeAuthenticationProvider的认证器userDetailsService
        AuthCenterAuthenticationProvider authCenterAuthenticationProvider = new AuthCenterAuthenticationProvider();
//        authCenterAuthenticationProvider.setUserDetailsService(userDetailsService);
//        wechatAuthenticationProvider.setPublicUserService(publicUserService);
//        wechatAuthenticationProvider.setWxUserRelationService(wxUserRelationService);
        //在UsernamePasswordAuthenticationFilter过滤前执行
        http.authenticationProvider(authCenterAuthenticationProvider)
                .addFilterAfter(authCenterAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
        ;

    }
}
